package com.java1412.service.impl;

import com.java1412.dto.HouseInfoDTO;
import com.java1412.entity.HouseInfo;
import com.java1412.mapper.HouseInfoMapper;
import com.java1412.service.HouseInfoService;
import com.java1412.utils.PageUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author java1412
 * @since 2025-04-30 13:57
 */
@Service
@Transactional
public class HouseInfoServiceImpl implements HouseInfoService {

    @Resource
    private HouseInfoMapper houseInfoMapper;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public PageUtil<HouseInfo> find(HouseInfoDTO houseInfoDTO) {
        PageUtil<HouseInfo> pageUtil = new PageUtil<>();
        houseInfoDTO.setStartIndexNo((houseInfoDTO.getStartPageNo() - 1) * houseInfoDTO.getPageSize());
        pageUtil.setCurrentPageNo(houseInfoDTO.getStartPageNo());
        pageUtil.setPageSize(houseInfoDTO.getPageSize());
        pageUtil.setTotalCount(houseInfoMapper.findCount(houseInfoDTO));
        pageUtil.setData(houseInfoMapper.find(houseInfoDTO));
        return pageUtil;
    }

    @Override
    public Map<String, Object> add(HouseInfoDTO houseInfoDTO) {
        Map<String, Object> map = new HashMap();
        houseInfoDTO.setPublishDate(new Date());
        Integer result = houseInfoMapper.add(houseInfoDTO);
        if (result.intValue() > 0) {
            map.put("code", 200);
            map.put("msg", "发布房源成功");
        } else {
            map.put("code", 500);
            map.put("msg", "发布房源失败");
        }
        return map;
    }
}
